#!/bin/bash
export CANAL_REGISTER_IP=`ip addr|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}' | sed 's/\/.*//g'`
echo "CANAL_REGISTER_IP: $CANAL_REGISTER_IP"

# if mac os, then  sed -i  "" 's/old/new/g' file
sed -i "s/CANAL_REGISTER_IP/${CANAL_REGISTER_IP}/g" /canal/conf/canal.properties
sed -i "s/CANAL_ZKSERVERS/${CANAL_ZKSERVERS}/g" /canal/conf/canal.properties
sed -i "s/CANAL_SERVERMODE/${CANAL_SERVERMODE}/g" /canal/conf/canal.properties
sed -i "s/CANAL_INSTANCE_FILTER_DRUID_DDL/${CANAL_INSTANCE_FILTER_DRUID_DDL}/g" /canal/conf/canal.properties
sed -i "s/CANAL_INSTANCE_FILTER_QUERY_DDL/${CANAL_INSTANCE_FILTER_QUERY_DDL}/g" /canal/conf/canal.properties
sed -i "s/CANAL_INSTANCE_FILTER_QUERY_DML/${CANAL_INSTANCE_FILTER_QUERY_DML}/g" /canal/conf/canal.properties
sed -i "s/CANAL_INSTANCE_FILTER_QUERY_DCL/${CANAL_INSTANCE_FILTER_QUERY_DCL}/g" /canal/conf/canal.properties
sed -i "s/CANAL_INSTANCE_FILTER_TABLE_ERROR/${CANAL_INSTANCE_FILTER_TABLE_ERROR}/g" /canal/conf/canal.properties
sed -i "s/KAFKA_BOOTSTRAP_SERVERS/${KAFKA_BOOTSTRAP_SERVERS}/g" /canal/conf/canal.properties
sed -i "s/KAFKA_ACKS/${KAFKA_ACKS}/g" /canal/conf/canal.properties
sed -i "s/KAFKA_COMPRESSION_TYPE/${KAFKA_COMPRESSION_TYPE}/g" /canal/conf/canal.properties
sed -i "s/KAFKA_BATCH_SIZE/${KAFKA_BATCH_SIZE}/g" /canal/conf/canal.properties
sed -i "s/KAFKA_MAX_REQUEST_SIZE/${KAFKA_MAX_REQUEST_SIZE}/g" /canal/conf/canal.properties
sed -i "s/KAFKA_BUFFER_MEMORY/${KAFKA_BUFFER_MEMORY}/g" /canal/conf/canal.properties
sed -i "s/KAFKA_RETRIES/${KAFKA_RETRIES}/g" /canal/conf/canal.properties
sed -i "s/CANAL_DESTINATIONS/${CANAL_DESTINATIONS}/g" /canal/conf/canal.properties

sed -i "s/MYSQL_SLAVEID/${MYSQL_SLAVEID}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_INSTANCE_MASTER_JOURNAL_NAME/${CANAL_INSTANCE_MASTER_JOURNAL_NAME}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_INSTANCE_MASTER_POSITION/${CANAL_INSTANCE_MASTER_POSITION}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_INSTANCE_MASTER_TIMESTAMP/${CANAL_INSTANCE_MASTER_TIMESTAMP}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_INSTANCE_GTIDON/${CANAL_INSTANCE_GTIDON}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_INSTANCE_MASTER_GTID/${CANAL_INSTANCE_MASTER_GTID}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_INSTANCE_MASTER_ADDRESS/${CANAL_INSTANCE_MASTER_ADDRESS}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_INSTANCE_DBUSERNAME/${CANAL_INSTANCE_DBUSERNAME}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_INSTANCE_DBPASSWORD/${CANAL_INSTANCE_DBPASSWORD}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_INSTANCE_DEFAULT_DATABASENAME/${CANAL_INSTANCE_DEFAULT_DATABASENAME}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_INSTANCE_FILTER_REGEX/${CANAL_INSTANCE_FILTER_REGEX}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_INSTANCE_FILTER_BLACK_REGEX/${CANAL_INSTANCE_FILTER_BLACK_REGEX}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_MQ_TOPIC/${CANAL_MQ_TOPIC}/g" /canal/conf/example/instance.properties
# sed -i "s/CANAL_MQ_SINGLE_PARTITION/${CANAL_MQ_SINGLE_PARTITION}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_MQ_PARTITIONSNUM/${CANAL_MQ_PARTITIONSNUM}/g" /canal/conf/example/instance.properties
sed -i "s/CANAL_MQ_PARTITIONHASH/${CANAL_MQ_PARTITIONHASH}/g" /canal/conf/example/instance.properties

# 判断存在meta.dat文件时, 是否修改meta文件

cd /canal
bash bin/stop.sh
bash bin/startup.sh
echo "修改实例文件:exampe => ${CANAL_DESTINATIONS}"
mv /canal/conf/example /canal/conf/${CANAL_DESTINATIONS}
if [ ! -d /canal/logs/${CANAL_DESTINATIONS} ]; then
    echo "文件夹不存在,创建中"
    mkdir -p /canal/logs/${CANAL_DESTINATIONS}
    touch /canal/logs/${CANAL_DESTINATIONS}/${CANAL_DESTINATIONS}.log
    else
    echo "文件夹已存在"
fi
echo "start successfuly!!!"
tail -f /canal/logs/${CANAL_DESTINATIONS}/*.log